package com.sixbro.shiro.service.sys;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sixbro.shiro.common.exception.BizException;
import com.sixbro.shiro.domain.entity.SysDict;
import com.sixbro.shiro.domain.entity.SysDictDetail;
import com.sixbro.shiro.mapper.SysDictDetailMapper;
import com.sixbro.shiro.mapper.SysDictMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * <p>
 *
 * </p>
 *
 * @Author: Mr.Lu
 * @Since: 2020/6/19 15:17
 */
@Service
public class SysDictDetailServiceImpl extends ServiceImpl<SysDictDetailMapper, SysDictDetail> implements SysDictDetailService {

    @Autowired
    private SysDictDetailMapper sysDictDetailMapper;

    @Autowired
    private SysDictMapper sysDictMapper;


    @Override
    public IPage<SysDictDetail> listByPage(Page page, String dictId) {

        SysDict sysDict = sysDictMapper.selectById(dictId);
        if (sysDict == null) {
            throw new BizException("获取字典数据失败!");
        }

        LambdaQueryWrapper<SysDictDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysDictDetail::getDictId, dictId);
        wrapper.orderByAsc(SysDictDetail::getSort);
        IPage<SysDictDetail> result = sysDictDetailMapper.selectPage(page, wrapper);
        if (!CollectionUtils.isEmpty(result.getRecords())) {
            result.getRecords().parallelStream().forEach(entity -> {
                entity.setDictName(sysDict.getName());
            });
        }
        return result;
    }
}
